//
//  TestFinishViewController.m
//  Learn
//
//  Created by 迟建国 on 2024/4/2.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "TestFinishViewController.h"
#import "HomeRequest.h"
#import "PlayManager.h"

@interface TestFinishViewController ()

@end

@implementation TestFinishViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [PlayManager sharPlayManager].useTime = 0;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = rgbHex(0xF5F5F5, 1);
    [self setUI];
    [self setWhiteBackButton];
    // Do any additional setup after loading the view.
}

- (void)setUI {
    
    UIImageView *topBgView = [[UIImageView alloc] init];
    topBgView.image = [UIImage imageNamed:@"icon_result_bg"];
    topBgView.backgroundColor = UIColor.grayColor;
    [self.view addSubview:topBgView];
    [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.offset(287 + lTopBarHeight);
    }];
    
    UILabel *scoreL = [[UILabel alloc] init];
    scoreL.text = [NSString stringWithFormat:@"%.2f", [self.data[@"score"] doubleValue]];
    scoreL.textColor = UIColor.whiteColor;
    scoreL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:66];
    [self.view addSubview:scoreL];
    [scoreL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(lTopBarHeight + 20);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    
    UILabel *desL = [[UILabel alloc] init];
    desL.text = [NSString stringWithFormat:@"%@", self.data[@"msg"]];
    desL.textColor = UIColor.whiteColor;
    desL.font = [UIFont fontWithName:@"PingFangSC" size:18];
    [self.view addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(scoreL.mas_bottom).offset(10);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    
    UILabel *statusL = [[UILabel alloc] init];
    statusL.text = @"作业提交成功";
    statusL.textColor = [UIColor whiteColor];
    statusL.font = [UIFont fontWithName:@"PingFangSC" size:18];
    [self.view addSubview:statusL];
    [statusL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(scoreL.mas_bottom).offset(30);
        make.centerX.equalTo(self.view.mas_centerX).offset(-15);
    }];
    
    UIImageView *statusImageView = [[UIImageView alloc] init];
    statusImageView.image = [UIImage imageNamed:@"icon_done"];
    [self.view addSubview:statusImageView];
    [statusImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(statusL.mas_centerY);
        make.left.equalTo(statusL.mas_right).offset(10);
        make.width.height.offset(18);
    }];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 15;
    bgView.layer.masksToBounds = YES;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
        make.top.equalTo(topBgView.mas_bottom).offset(-57);
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.height.mas_equalTo(254);
    }];
    
    UILabel *allScoreL = [[UILabel alloc] init];
    allScoreL.text = @"总分";
    allScoreL.textColor = rgba(51, 51, 51, 1);
    allScoreL.textAlignment = NSTextAlignmentCenter;
    allScoreL.font = [UIFont fontWithName:@"PingFangSC" size:16];
    [bgView addSubview:allScoreL];
    [allScoreL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView.mas_top).offset(5);
        make.left.equalTo(bgView.mas_left).offset(10);
        make.height.mas_equalTo(52);
    }];
    UILabel *scoreValueL = [[UILabel alloc] init];
    scoreValueL.text = [NSString stringWithFormat:@"%.2f", [self.data[@"full_score"] doubleValue]];
    scoreValueL.textColor = rgba(51, 51, 51, 1);
    scoreValueL.textAlignment = NSTextAlignmentCenter;
    scoreValueL.font = [UIFont fontWithName:@"PingFangSC" size:16];
    [bgView addSubview:scoreValueL];
    [scoreValueL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(allScoreL.mas_centerY);
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.height.mas_equalTo(52);
    }];
    UIView *scoreLineView = [[UIView alloc] init];
    scoreLineView.backgroundColor = rgba(229, 229, 229, 1);
    [bgView addSubview:scoreLineView];
    [scoreLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(10);
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.top.equalTo(allScoreL.mas_bottom).offset(0);
        make.height.mas_equalTo(1);
    }];
    
    UILabel *timeL = [[UILabel alloc] init];
    timeL.text = @"作答用时";
    timeL.textColor = rgba(51, 51, 51, 1);
    timeL.textAlignment = NSTextAlignmentCenter;
    timeL.font = [UIFont fontWithName:@"PingFangSC" size:16];
    [bgView addSubview:timeL];
    [timeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(scoreLineView.mas_bottom).offset(0);
        make.left.equalTo(bgView.mas_left).offset(10);
        make.height.mas_equalTo(52);
    }];
    
    UILabel *timeValueL = [[UILabel alloc] init];
    timeValueL.text = [NSString stringWithFormat:@"%@",self.data[@"use_time"]];
    timeValueL.textColor = rgba(51, 51, 51, 1);
    timeValueL.textAlignment = NSTextAlignmentCenter;
    timeValueL.font = [UIFont fontWithName:@"PingFangSC" size:16];
    [bgView addSubview:timeValueL];
    [timeValueL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(timeL.mas_centerY);
        make.right.equalTo(bgView.mas_right).offset(-10);
    }];
    UIView *timeLineView = [[UIView alloc] init];
    timeLineView.backgroundColor = rgba(229, 229, 229, 1);
    [bgView addSubview:timeLineView];
    [timeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(10);
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.top.equalTo(timeL.mas_bottom).offset(0);
        make.height.mas_equalTo(1);
    }];
    
    UILabel *detailL = [[UILabel alloc] init];
    detailL.text = @"查看详情";
    detailL.textColor = rgba(51, 51, 51, 1);
    detailL.textAlignment = NSTextAlignmentCenter;
    detailL.font = [UIFont fontWithName:@"PingFangSC" size:16];
    [bgView addSubview:detailL];
    [detailL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeLineView.mas_bottom).offset(0);
        make.left.equalTo(bgView.mas_left).offset(10);
        make.height.mas_equalTo(52);
    }];
    UIButton *detailBtn = [UIButton new];
    [detailBtn setBackgroundColor:lClearColor];
    [detailBtn addTarget:self action:@selector(goDetailVC) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:detailBtn];
    
    UIImageView *rightImage = [[UIImageView alloc] init];
    rightImage.image = [UIImage imageNamed:@"jiantou"];
    [bgView addSubview:rightImage];
    [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(detailL.mas_centerY);
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.height.mas_equalTo(12);
        make.width.mas_equalTo(7);
    }];
    
    [detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeLineView.mas_bottom).offset(0);
        make.left.equalTo(bgView.mas_left).offset(10);
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.height.mas_equalTo(52);
    }];
    
    UIView *detailLineView = [[UIView alloc] init];
    detailLineView.backgroundColor = rgba(229, 229, 229, 1);
    [bgView addSubview:detailLineView];
    [detailLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(10);
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.top.equalTo(detailL.mas_bottom).offset(0);
        make.height.mas_equalTo(1);
    }];
    
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(28,194,lScreenWidth - 76,45);
    gl.startPoint = CGPointMake(1, 0.5);
    gl.endPoint = CGPointMake(0, 0.5);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:41/255.0 green:241/255.0 blue:156/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:2/255.0 green:161/255.0 blue:249/255.0 alpha:1.0].CGColor];
    gl.locations = @[@(0), @(1.0f)];
    gl.cornerRadius = 22.5;
    gl.masksToBounds = YES;
    [bgView.layer addSublayer:gl];
    
    
    UIButton *finishBtn = [UIButton new];
    finishBtn.frame =CGRectMake(28, 194, lScreenWidth - 76, 45);
    
    [finishBtn setTitle:@"完成" forState:UIControlStateNormal];
   
    [finishBtn setTitleColor:lWhiteColor forState:UIControlStateNormal];
    [finishBtn.titleLabel setFont:MFontWithSize(18)];
    [finishBtn addTarget:self action:@selector(finish) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:finishBtn];
    
    
}



- (void)finishBtnClicked {
    [AlertView alertWith:self title:@"切换教材版本后，现有的计划将会被 新计划" content:@"" cancel:@"取消" sure:@"确定" Complection:^{
        
    }];
}

- (void)finish {
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([NSStringFromClass([vc class]) isEqualToString:@"BookDetailViewController"]) {
            [self.navigationController popToViewController:vc animated:YES];
            return;
        }
    }
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)back{
    [self finish];
}
- (void)goDetailVC
{
    UIViewController *vc = [[UIViewController alloc] initWithClassName:@"TestResultDetailViewController"];
    vc.title = self.title;
    vc.paraObject = self.data;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setParaObject:(NSObject *)paraObject
{
    self.data = (NSDictionary *)paraObject;
}
@end
